import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login')
  },
  {
    path: '/404',
    name: 'page404',
    component: () => import('@/views/page404')
  },
  {
    path: '/home',
    name: 'home',
    redirect:'/welcome',
    component: () => import('@/views/home'),
    children: [
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import('@/views/component2/welcome'),
      },
      //用户管理
      {
        path: '/users',
        name: 'users',
        component: () => import('@/components/users/users'),
      },
      //权限管理
      {
        path: '/roles',
        name: 'roles',
        component: () => import('@/components/power/roles'),
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('@/components/power/rights'),
      },
      //商品管理
      {
        path: '/goods',
        name: 'goods',
        component: () => import('@/components/goods/goods'),
      },
      {
        path: '/add',
        name: 'addGoods',
        component: () => import('@/components/goods/addGoods'),
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('@/components/goods/params'),
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('@/components/goods/categories'),
      },
      //订单管理
      {
        path: '/orders',
        name: 'orders',
        component: () => import('@/components/orders/orders'),
      },
      //数据统计
      {
        path: '/reports',
        name: 'reports',
        component: () => import('@/components/reports/reports'),
      },
    ]
  },

  {
    path: '*',
    redirect: '/login'
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  let token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  //token值存在的情况下，可以访问任何页面
  next()
})

export default router
